<?php

namespace app\user\controller;

use think\facade\Hook;
use think\Request;

class Setting extends Base
{

    public function index()
    {
        //  Hook::add('user_setting', ['addon\\weiboLogin\\controller\\Index','addon\\weixinLogin\\controller\\Index']);

        if (!is_login()) {
            $this->error('请登录');
        }

        return view('index');
    }


    public function updateInfo()
    {
        $uid = is_login();

        $nickname = input('nickname', '', 'text');
        $sex = input('sex', 0, 'intval');
        $birthday = input('birthday', '', 'text');
        $signature = input('signature', '', 'text');

        $data = [
            'nickname' => $nickname,
            'sex' => $sex,
            'birthday' => $birthday,
            'signature' => $signature
        ];
        model('user/User')->updateUserInfo($uid, $data);

        cache('user_' . $uid, null);
        $this->success('修改成功');
        //return json(['info' => '修改成功~', 'status' => 1]);

    }


    public function resetPwd()
    {
        $nowpwd = input('nowpwd', '', 'text');
        $pwd = input('pwd', '', 'text');
        $repwd = input('repwd', '', 'text');

        $uid = $this->checkIsLogin();

        if (!check_password_l($pwd)) {
            $this->error('新密码格式不正确!');
        }

        if ($pwd != $repwd) {
            $this->error('两次输入的密码不一致!');
        }
        if ($pwd == $nowpwd) {
            $this->error('新密码不能与旧密码相同!');
        }


        $userModel = model('user/User');
        $info = $userModel->getUserInfo($uid);

        if ($info['password'] != $userModel->setPasswordAttr($nowpwd)) {
            $this->error('当前密码有误!');
        }

        $data['password'] = $userModel->setPasswordAttr($pwd);
        model('user/User')->updateUserInfo($uid, $data);
        cache('user_' . $uid, null);

        $this->success('修改成功');


    }


    public function unbind()
    {
        $uid = $this->checkIsLogin();
        $type = input('type', '', 'text');


        $data[$type] = '';
        model('user/User')->updateUserInfo($uid, $data);
        cache('user_' . $uid, null);
        $this->success('解绑成功');
    }

}
